/*
 * Copyright (c) 1999-2003, Carnegie Mellon University. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Products derived from the software may not be called "iVProg",
 *    nor may "iVProg" appear in their name, without prior written
 *    permission of Carnegie Mellon University.
 *
 * 4. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes software developed by Carnegie Mellon University"
 */

package edu.cmu.cs.stage3.alice.core;

import edu.cmu.cs.stage3.alice.core.property.BooleanProperty;
import edu.cmu.cs.stage3.alice.core.property.ElementArrayProperty;

public class Transformable extends ReferenceFrame {

 public final ElementArrayProperty parts = new ElementArrayProperty( this, "parts", null, Transformable[].class );
 public final BooleanProperty isPivotShowing = new BooleanProperty( this, "isPivotShowing", Boolean.FALSE );

 public Transformable () {
  super();
  }

 //Rem @Override
 protected void internalRelease( int pass) { }

 private Transformable internalGetPartNamed (String nameValue, boolean ignoreCase) {
  for (int i=0; i<parts.size(); i++) {
   Transformable part = (Transformable)parts.get( i );
   if (nameValue!=null) {
    boolean found;
    if (ignoreCase) {
     found = nameValue.equalsIgnoreCase( part.name.getStringValue() );
    } else {
     found = nameValue.equals( part.name.getStringValue() );
     }
    if (found) {
     return part;
     }
   } else {
    if (part.name.getStringValue()==null) {
     return part;
     }
    }
   }
  return null;
  }

 private Transformable internalGetPartKeyed (String key, int fromIndex, boolean ignoreCase) {
  if (key.equals( "" )) {
   return this;
  } else {
   int toIndex = key.indexOf( SEPARATOR, fromIndex );
   if (toIndex == -1) {
    String childName = key.substring( fromIndex );
    return internalGetPartNamed( childName, ignoreCase );
   } else {
    String childName = key.substring( fromIndex, toIndex );
    Transformable child = internalGetPartNamed( childName, ignoreCase );
    if (child != null) {
     return child.internalGetPartKeyed( key, toIndex+1, ignoreCase );
    } else {
     return null;
     }
    }
   }
  }

 public Element getPartKeyed (String key) {
  return internalGetPartKeyed( key, 0, false );
  }

 public Element getPartKeyedIgnoreCase (String key) {
  return internalGetPartKeyed( key, 0, true );
  }

 //Rem @Override
 protected void propertyChanging (Property property, Object value) { }
 //Rem @Override
 protected void propertyChanged (Property property, Object value) { }

 public void setVehiclePreservingAbsoluteTransformation (ReferenceFrame vehicleValue) { }

 }
